package com.droowa.controller;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.droowa.domain.BusinessRecord;
import com.droowa.domain.Customer;
import com.droowa.domain.DeliveryEnterpriseManagement;
import com.droowa.domain.DeliveryOption;
import com.droowa.domain.MileageOption;
import com.droowa.domain.MyShop;
import com.droowa.domain.PaymentManagement;
import com.droowa.domain.ShopManager;
import com.droowa.model.service.BusinessRecordService;
import com.droowa.model.service.CustomerService;
import com.droowa.model.service.DeliveryEnterpriseManagementService;
import com.droowa.model.service.DeliveryOptionService;
import com.droowa.model.service.MileageOptionService;
import com.droowa.model.service.MyShopService;
import com.droowa.model.service.PaymentManagementService;
import com.droowa.model.service.ShopManagerService;

@Controller
@RequestMapping("/myshop/")
public class MyShopController {
	
	@Autowired
	private MyShopService mSService;
	@Autowired
	private ShopManagerService sMService;
	@Autowired
	private BusinessRecordService bRService;
	@Autowired
	private MileageOptionService mOService;
	@Autowired
	private PaymentManagementService pMService;
	@Autowired
	private DeliveryOptionService dOService;
	@Autowired
	private DeliveryEnterpriseManagementService dEMService;
	@Autowired
	private CustomerService cService;
	
	@RequestMapping(value="registerMyShop.do", method=RequestMethod.GET)
	public String registerMyShopForm(HttpServletRequest request) throws SQLException{
		request.setAttribute("menu_category", "store");
		request.setAttribute("myshop", mSService.getMyShop());
		return "admin.myshop/register_my_shop";
	}
	@RequestMapping(value="registerMyShop.do", method=RequestMethod.POST)
	public String registerMyShop(MyShop myShop, ModelMap map, HttpSession session) throws Exception{
			int success = mSService.joinMyShop(myShop);
			setSuccess(success, map, "상점", "상점정보", session);
			return "jsonView";
	}
	
	@RequestMapping("getShopManager.do")
	public String getShopManagerForm(HttpServletRequest request) throws SQLException{
		request.setAttribute("menu_category", "store");
		request.setAttribute("shopManager", sMService.getShopManager());
		return "admin.myshop/get_shop_manager";
	}
	
	@RequestMapping(value="registerShopManager.do", method=RequestMethod.GET)
	public String registerShopManagerForm(HttpServletRequest request) throws SQLException{
		return "/body/admin/myshop/register_shop_manager.jsp";
	}
	@RequestMapping(value="registerShopManager.do", method=RequestMethod.POST)
	public String registerShopManager(ShopManager shopManager, ModelMap map, HttpSession session) throws Exception{
		int success = sMService.joinShopManager(shopManager);
		setSuccess(success, map, "상점", shopManager.getSort(), session);
		return "jsonView";
	}

	@RequestMapping(value="getBusinessRecordForm.do", method=RequestMethod.GET)
	public String getBusinessRecordForm(HttpServletRequest request){   
		request.setAttribute("menu_category", "store");
		return "admin.myshop/get_business_record";
	}
	@RequestMapping("getBusinessSort.do")
	public String getBusinessSort(HttpServletRequest request, ModelMap map){
		String firstSort = request.getParameter("firstSort");
		ArrayList list = new ArrayList();
		if(firstSort.equals("관리자")){
			list.add("로그인");
			list.add("로그아웃");
		}else if(firstSort.equals("회원")){
			list.add("로그인");
			list.add("로그아웃");
			list.add("회원가입");
			list.add("회원정보수정");
			list.add("회원정보 다운로드");
		}else if(firstSort.equals("상점")){
			list.add("상점정보등록");				
			list.add("상점정보수정");
			list.add("마일리지설정등록");				
			list.add("마일리지설정수정");
			list.add("결제설정등록");				
			list.add("결제설정수정");
			list.add("배송업체등록");				
			list.add("배송업체수정");
			list.add("대표운영자등록");
			list.add("대표운영자수정");
			list.add("대표운영자삭제");
			list.add("부운영자등록");
			list.add("부운영자수정");
			list.add("부운영자삭제");
			list.add("관리자등록");
			list.add("관리자수정");
			list.add("관리자삭제");
		}else if(firstSort.equals("상품")){
			list.add("카테고리등록");
			list.add("카테고리수정");
			list.add("카테고리삭제");
			list.add("상품등록");
			list.add("상품수정");
			list.add("상품삭제");
			list.add("완제품등록");
			list.add("완제품수정");
			list.add("완제품삭제");
			list.add("이벤트상품등록");
			list.add("이벤트상품수정");
			list.add("이벤트상품삭제");
			list.add("상품완전삭제");
			list.add("상품전체정보 다운로드");
		}else if(firstSort.equals("주문")){
			list.add("주문정보 다운로드");
			list.add("입금확인");
			list.add("미입금처리");
			list.add("배송시작");
			list.add("배송완료");
			list.add("취소");
			list.add("교환");
			list.add("반품");
			list.add("환불");
			list.add("주문서 삭제");
			list.add("주문정보변경");
		}else if(firstSort.equals("게시판")){
			list.add("게시물등록");
			list.add("게시물수정");
			list.add("게시물삭제");
			list.add("답변등록");
			list.add("답변수정");
			list.add("답변삭제");
			list.add("댓글등록");
			list.add("댓글수정");
			list.add("댓글삭제");
			list.add("긴급문의답변");
		}else if(firstSort.equals("다운로드")){
			list.add("통합엑셀다운로드");
		}
		map.addAttribute("list", list);
		return "jsonView";
	}
	
	@RequestMapping(value="getBusinessRecord.do")
	public ModelAndView getBusinessRecord(String sort, String sort2, String startDate, String endDate,
			@RequestParam(defaultValue="1") int page, HttpServletRequest request, HttpSession session) throws Exception{
		request.setAttribute("menu_category", "store");
		System.out.println("-------------------");
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		System.out.println(sort+sort2+startDate+endDate);
		request.setAttribute("sort", sort);
		request.setAttribute("sort2", sort2);
		request.setAttribute("startDate", startDate);
		request.setAttribute("endDate", endDate);
		HashMap map = new HashMap();
		map.put("sort", sort);
		map.put("sort2", sort2);
		map.put("startDate", startDate);
		map.put("endDate", endDate+sdf.format(new Date()));
		map.put("page", page);
		
		Map resultMap = bRService.getBusinessRecordByAll(map);
		session.setAttribute("result", resultMap.get(""));
		
		
		return new ModelAndView("admin.myshop/get_business_record", resultMap);
	}
	
	@RequestMapping(value="registerMileageOption.do", method=RequestMethod.GET)
	public String registerMileageOptionForm(HttpServletRequest request) throws SQLException{   
		request.setAttribute("menu_category", "store");
		request.setAttribute("mileage", mOService.getMileageOption());
		return "admin.myshop/register_mileage_option";
	}
	@RequestMapping(value="registerMileageOption.do", method=RequestMethod.POST)
	public String registerMileageOption(MileageOption mileageOption, ModelMap map, HttpSession session) throws Exception{
		int success = mOService.joinMileageOption(mileageOption);
		setSuccess(success, map, "상점", "마일리지설정", session);
		return "jsonView";
	}
	
	@RequestMapping(value="getPaymentManagement.do", method=RequestMethod.GET )
	public String getPaymentManagement(HttpServletRequest request) throws SQLException{   
		request.setAttribute("menu_category", "store");
		request.setAttribute("payment", pMService.getPaymentManagement());
		return "admin.myshop/get_payment_management";
	}
	@RequestMapping(value="registerPaymentManagement.do", method=RequestMethod.GET)
	public String registerPaymentManagementForm(PaymentManagement paymentManagement, ModelMap map) throws Exception{
		return "/body/admin/myshop/register_payment_management.jsp";
	}
	@RequestMapping(value="registerPaymentManagement.do", method=RequestMethod.POST)
	public String registerPaymentManagement(PaymentManagement paymentManagement, ModelMap map, HttpSession session) throws Exception{
		int success = pMService.joinPaymentManagement(paymentManagement);
		setSuccess(success, map, "상점", "결제설정", session);
		return "jsonView";
	}
	
	@RequestMapping(value="registerDeliveryOption.do", method=RequestMethod.GET)
	public String registerDeliveryOptionForm(HttpServletRequest request){   
		request.setAttribute("menu_category", "store");
		return "admin.myshop/register_delivery_option";
	}
	@RequestMapping(value="registerDeliveryOption.do", method=RequestMethod.POST)
	public String registerDeliveryOption(DeliveryOption deliveryOption, ModelMap map) throws Exception{
		int success = dOService.joinDeliveryOption(deliveryOption);
		if(success==1){
			map.addAttribute("success", success);
		}
		return "jsonView";
	}
	
	@RequestMapping("getDeliveryEnterpriseManagementForm.do")
	public String getDeliveryEnterpriseManagementForm(HttpServletRequest request) throws SQLException{
		request.setAttribute("menu_category", "store");
		request.setAttribute("deliveryEnterprise", dEMService.getDeliveryEnterpriseManagement());
		return "admin.myshop/get_delivery_enterprise_management_form";
	}
	@RequestMapping(value="registerDeliveryEnterprise.do", method=RequestMethod.GET)
	public String registerDeliveryEnterpriseManagementForm(HttpServletRequest request){   
		return "/body/admin/myshop/register_delivery_enterprise_management.jsp";
	}
	@RequestMapping(value="registerDeliveryEnterprise.do", method=RequestMethod.POST)
	public String registerDeliveryEnterprise(DeliveryEnterpriseManagement  deliveryEnterpriseManagement, ModelMap map, HttpSession session) throws SQLException{
		int success = dEMService.joinDeliveryEnterpriseManagement(deliveryEnterpriseManagement);
		setSuccess(success, map, "상점", "배송업체", session);	
		return "jsonView";
	}
	
	//success를 판단하여 map에 success값을 bind시켜주는 메소드
	private void setSuccess(int success, ModelMap map, String sort, String sort2, HttpSession session) throws SQLException {
		ShopManager sm = (ShopManager)session.getAttribute("login_info_manager");
		if(success==1){//성공 시
			map.addAttribute("success", 1);
			bRService.joinBusinessRecord(new BusinessRecord(null, sort, sort2+"등록", new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()), sm.getSort()));
		}else if(success==-1){//업데이트 시
			map.addAttribute("success", success);
			BusinessRecord br = new BusinessRecord(null, sort, sort2+"수정", new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()), sm.getSort());
			bRService.joinBusinessRecord(br);
		}else{//실패 시
			map.addAttribute("success", success);
		}
	}
	
	@RequestMapping("chart.do")
	public ModelAndView chart() throws SQLException{
		List<Customer> list = cService.getCustomerList();
		return new ModelAndView("admin.myshop/chart", "info", list);
	}
	
	//=====================Excel 파일 생성=======================
	@RequestMapping("businessRecordView.do")
	public String businessRecordView(Map<String, Object> model, HttpSession session) throws SQLException{
		List<BusinessRecord> list = (List<BusinessRecord>)session.getAttribute("result");
		model.put("businessRecord", list);
		return "businessRecordView";
	}
	
}
